package com.seo.controller.transaction;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.deduc.impl.DeducServiceImpl;
import com.seo.service.proxy.deduc.impl.ProxyDeducServiceImpl;
import com.seo.utils.CommonUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: DeducController
 * @author sbl
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/deduc/")
public class DeducController {

	@Autowired
	private ProxyDeducServiceImpl proxyDeducService;
	
	@Autowired
	private DeducServiceImpl deducService;

	@RequestMapping(value = "/child/add")
	public String addChild() {
		log.info("添加扣费界面");
		if(!Jurisdiction.roleCode("proxy")){
			throw new DefiniteException("请先升级为代理");
		}
		return "transaction/child/deduc_add";
	}

	@RequestMapping("/child/addDeduc")
	@ResponseBody
	public ReturnBody addChildDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.roleCode("proxy")) {
			return rb.error("error", "权限不足");
		}
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-保存扣费明细", tradeId);
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("parentId", userId);
		return deducService.saveDeducDetail(tradeId, params);
	}

	@RequestMapping(value = "/proxy/add")
	public String add() {
		log.info("添加扣费界面");
		if(!Jurisdiction.roleCode("proxy")){
			throw new DefiniteException("请先升级为代理");
		}
		return "transaction/proxy/deduc_add";
	}

	@RequestMapping("/proxy/addDeduc")
	@ResponseBody
	public ReturnBody addDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.roleCode("proxy")) {
			return rb.error("error", "权限不足");
		}
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-保存扣费明细", tradeId);
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		return proxyDeducService.saveProxyDeducDetail(tradeId, params);
	}

}
